#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>

#define PLUGIN_NAME                   "l4d2_Witch_attacker_scope"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "Witch 普通攻击带秒杀范围"   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站




public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

Handle l4d2_witch_killdis;

float Trigger_Witch;

int g_Witch_hdgfbn;

ConVar hCvar_Witch_ffvcd;

public void OnPluginStart()
{
	RegAdminCmd("sm_witchms", Command_SmokerSwitch, ADMFLAG_KICK, "Witch秒杀范围指令开关");
	hCvar_Witch_ffvcd = CreateConVar("l4d2_hCvar_Witch_ffvcd", "1", "是否启用Witch秒杀范围插件", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	HookConVarChange(hCvar_Witch_ffvcd, eConvarChanged);	


	l4d2_witch_killdis = CreateConVar("l4d2_witch_killdis_pkd", "500.0", "玩家与Witch多少距离之内，玩家被秒杀?", FCVAR_NOTIFY);
	HookConVarChange(l4d2_witch_killdis, eConvarChanged);
	
	//AutoExecConfig(true, "l4d2_Witch_attacker_scope");



	HookEvent("player_hurt",				Event_PlayerHurt,		EventHookMode_Pre); //玩家受伤
	HookEvent("player_incapacitated",		Event_PlayerHurt,		EventHookMode_Pre);  //玩家倒地事件
}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	Trigger_Witch = GetConVarFloat(l4d2_witch_killdis);
	g_Witch_hdgfbn = hCvar_Witch_ffvcd.IntValue;	
}


public Action Command_SmokerSwitch(int client, int args)
{
	if (g_Witch_hdgfbn)
	{
		hCvar_Witch_ffvcd.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x03Witch\x05范围秒杀插件已\x03关闭\x05.");
	}
	else
	{
		hCvar_Witch_ffvcd.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x03Witch\x05范围秒杀插件已\x03开启\x05.");
	}
	return Plugin_Handled;
}

/* 玩家受伤 || 玩家倒地 */
void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{	
	int victim = GetClientOfUserId(event.GetInt("userid"));
	int attackerentid = event.GetInt("attackerentid");	
		
	char classname[20];
	if(IsValidEdict(attackerentid))
	{
		GetEdictClassname(attackerentid, classname, 20);
		if(StrEqual(classname, "witch", false))
		{
			if(IsPlayerAlive(victim) && g_Witch_hdgfbn == 1)
			{
				//PrintToChatAll("\x04[提示]\x03Witch\x05非常愤怒，发动了\x03秒杀\x05！");
				float pos[3];
				float entpos[3];
				float distance;
				GetEntPropVector(attackerentid, Prop_Send, "m_vecOrigin", pos);
				
				for (int i = 1; i <= MaxClients; i++)
				{
						if (!IsSurvivor(i) || !IsPlayerAlive(i))
						continue;
						GetEntPropVector(i, Prop_Send, "m_vecOrigin", entpos);
						distance = GetVectorDistance(pos, entpos);
						if (distance<=Trigger_Witch)
						{
							ForcePlayerSuicide(i);
							PrintToChatAll("\x04[提示]\x03Witch\x05杀死了\x03%N\x05.",  i);
						}
				}		
			}	
		}
	}
}	

bool IsSurvivor(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}























